<!-- 2017-12-6 14:11:44 | 修改 木遥（微信:  http://marsgis.cn/weixin.html ） -->
<!DOCTYPE html>
<html>

<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  <meta name="viewport" content="width=device-width,initial-scale=1,user-scalable=0,minimum-scale=1.0,maximum-scale=1.0" />
  <meta name="apple-touch-fullscreen" content="yes">
  <meta name="apple-mobile-web-app-capable" content="yes">
  <meta name="apple-mobile-web-app-status-bar-style" content="black">
  <meta name="format-detection" content="telephone=no">
  <meta name="x5-fullscreen" content="true">
  <meta http-equiv="X-UA-Compatible" content="IE=Edge,chrome=1" />
  <!-- 标题及搜索关键字 -->
  <meta name="keywords" content="火星科技,cesium,3D,GIS,marsgis,三维,地球,地图,开发,框架,系统,示例,资料,模型,离线,外包,合肥,安徽,中国" />
  <meta name="description"
        content="火星科技 合肥火星 合肥火星科技 合肥火星科技有限公司 leaflet leaflet框架 leaflet开发 cesium cesium开发 cesium框架 三维 地球 模型  gis marsgis 地图离线 地图开发 地图框架 地图外包 框架 开发 外包  地图离线 二维地图 三维地图 全景漫游 地理信息系统 云GIS 三维GIS GIS平台 WebGIS" />

  <link rel="shortcut icon" type="image/x-icon" href="http://mars3d.cn/favicon.ico">
  <title>电网 示例 | Mars3D | 三维地图 | 火星科技 | 合肥火星科技有限公司</title>


  <script type="text/javascript" src="../lib/include-lib.js" libpath="../lib/"
    include="jquery,font-awesome,bootstrap,layer,haoutil,turf,mars3d"></script>

  <link href="css/style.css" rel="stylesheet" />
</head>

<body class="dark">
  <!--加载前进行操作提示，优化用户体验-->
  <div id="mask" class="signmask" onclick="removeMask()"></div>

  <div id="mars3dContainer" class="mars3d-container"></div>


  <script src="./js/common.js"></script>
  <script type="text/javascript">
    'use script' //开发环境建议开启严格模式

    var map
    var graphicLayer

    function initMap(options) {
      //合并属性参数，可覆盖config.json中的对应配置
      var mapOptions = mars3d.Util.merge(options, {
        scene: {
          center: {
            y: 29.523284,
            x: 119.823365,
            z: 579.3,
            heading: 177.9,
            pitch: -27.3,
            roll: 0,
          },
          fxaa: true,
        },
      })

      //创建三维地球场景
      map = new mars3d.Map('mars3dContainer', mapOptions)

      //创建Graphic图层
      graphicLayer = new mars3d.layer.GraphicLayer()
      map.addLayer(graphicLayer)

      $.ajax({
        url: 'http://data.marsgis.cn/file/apidemo/tower.json',
        type: 'GET',
        dataType: 'json',
        success: function (result) {
          showData(result.data)
        },
      })
    }

    function showData(arrdata) {
      var polylines1 = [],
        polylines2 = [],
        polylines3 = [],
        polylines4 = [],
        polylines5 = []
      for (var i = 0; i < arrdata.length; i++) {
        var item = arrdata[i]

        //所在经纬度坐标及海拔高度
        var longitude = Number(item.longitude)
        var latitude = Number(item.latitude)
        var height = Number(item.height)

        var originPoint = {
          longitude: longitude,
          latitude: latitude,
          height: height,
        }
        var position = Cesium.Cartesian3.fromDegrees(originPoint.longitude, originPoint.latitude, originPoint.height)

        //计算电线塔转角角度
        var degree = parseInt(item.degree)

        //5条线路坐标
        var newPoint1 = mars3d.PointUtil.getTranslationPosition(
          position,
          {
            x: 0.341789,
            y: 16.837972,
            z: 50.717621,
          },
          degree,
          'z'
        )
        var newPoint2 = mars3d.PointUtil.getTranslationPosition(
          position,
          {
            x: 0.34241,
            y: -16.838163,
            z: 50.717617,
          },
          degree,
          'z'
        )
        var newPoint3 = mars3d.PointUtil.getTranslationPosition(
          position,
          {
            x: -0.025005,
            y: 0.022878,
            z: 39.540545,
          },
          degree,
          'z'
        )
        var newPoint4 = mars3d.PointUtil.getTranslationPosition(
          position,
          {
            x: -0.024999,
            y: 15.009109,
            z: 39.303012,
          },
          degree,
          'z'
        )
        var newPoint5 = mars3d.PointUtil.getTranslationPosition(
          position,
          {
            x: -0.025001,
            y: -15.009585,
            z: 39.301099,
          },
          degree,
          'z'
        )

        if (i === 0) {
          polylines1.push(newPoint1)
          polylines2.push(newPoint2)
          polylines3.push(newPoint3)
          polylines4.push(newPoint4)
          polylines5.push(newPoint5)
        } else {
          var angularityFactor = -5000,
            num = 50
          var positions = mars3d.PolyUtil.getLinkedPointList(polylines1[polylines1.length - 1], newPoint1, angularityFactor, num) //计算曲线点
          polylines1 = polylines1.concat(positions)

          positions = mars3d.PolyUtil.getLinkedPointList(polylines2[polylines2.length - 1], newPoint2, angularityFactor, num) //计算曲线点
          polylines2 = polylines2.concat(positions)

          positions = mars3d.PolyUtil.getLinkedPointList(polylines3[polylines3.length - 1], newPoint3, angularityFactor, num) //计算曲线点
          polylines3 = polylines3.concat(positions)

          positions = mars3d.PolyUtil.getLinkedPointList(polylines4[polylines4.length - 1], newPoint4, angularityFactor, num) //计算曲线点
          polylines4 = polylines4.concat(positions)

          positions = mars3d.PolyUtil.getLinkedPointList(polylines5[polylines5.length - 1], newPoint5, angularityFactor, num) //计算曲线点
          polylines5 = polylines5.concat(positions)
        }

        var html = mars3d.Util.getTemplateHtml({
          title: '塔杆',
          template: [
            { field: 'roadName', name: '所属线路' },
            { field: 'towerId', name: '杆塔编号' },
            { field: '杆塔型号', name: '杆塔型号' },
            { field: '杆塔性质', name: '杆塔性质' },
            { field: '杆塔类型', name: '杆塔类型' },
            { field: '投运日期', name: '投运日期' },
            { field: '杆塔全高', name: '杆塔全高' },
            { field: '设计单位', name: '设计单位' },
            { field: '施工单位', name: '施工单位' },
            { field: 'height', name: '海拔高度' },
          ],
          attr: item,
        })

        drawWireTowerModel(position, degree, html)
      }

      //绘制路线
      drawGuideLine(polylines1, '#ffffff')
      drawGuideLine(polylines2, '#ffffff')
      drawGuideLine(polylines3)
      drawGuideLine(polylines4, '#ff0000')
      drawGuideLine(polylines5, '#0000ff')
    }

    //绘制电线塔模型
    function drawWireTowerModel(position, degree, inthtml) {
      var modelUrls = ['tower.glb', 'V.glb', 'vertical01.glb', 'vertical02.glb']
      for (var j = 0; j < modelUrls.length; j++) {
        var primitive = new mars3d.graphic.ModelPrimitive({
          position: position,
          style: {
            url: 'http://data.marsgis.cn/gltf/mars/tower/' + modelUrls[j],
            heading: degree,
            scale: 1,
            distanceDisplayCondition: new Cesium.DistanceDisplayCondition(0, 4000.0),
          },
        })
        graphicLayer.addGraphic(primitive)

        primitive.bindPopup(inthtml, { anchor: [0, -12] })
      }
    }

    function drawGuideLine(positions, color) {
      var primitive = new mars3d.graphic.PolylinePrimitive({
        positions: positions,
        style: {
          width: 4,
          material: mars3d.MaterialUtil.createMaterial(mars3d.MaterialType.Color, {
            color: color || '#ffff00',
          }),
        },
      })
      graphicLayer.addGraphic(primitive)
    }
  </script>
</body>

</html>
